﻿#include <sstream>
#include "scene.h"
#include "command.h"

CCommand::CCommand(CScene *pOwner) : _pOwner(pOwner)
{}

CCommand::CCommand(CScene *pOwner, const point_t &point, int preValue, int curValue)
    : _pOwner(pOwner), _stPoint(point), _nPreValue(preValue), _nCurValue(curValue) {}

CCommand::CCommand(const CCommand &rhs)
: _pOwner(rhs._pOwner)
, _stPoint(rhs._stPoint)
, _nPreValue(rhs._nPreValue)
, _nCurValue(rhs._nCurValue)
{}

CCommand::~CCommand(){}

bool CCommand::execute(int nInputValue)
{
    if (!_pOwner)
        return false;

    _stPoint = _pOwner->getCurPoint();
    return _pOwner->setCurValue(nInputValue, _nPreValue);
}
void CCommand::processCommand(const std::string& command) {
    std::istringstream iss(command);
    std::string cmd;
    iss >> cmd;

    if (cmd == "remove") {
        int row, col;
        if (iss >> row >> col) {
            _scene->removeNumber(row, col);
        } else {
            std::cout << "Invalid command format. Usage: remove <row> <col>\n";
        }
    } else if (cmd == "decreaseScore") {
        int amount;
        if (iss >> amount) {
            _scene->decreaseScore(amount);
        } else {
            std::cout << "Invalid command format. Usage: decreaseScore <amount>\n";
        }
    } else {
        std::cout << "Unknown command: " << cmd << "\n";
    }
}
void CCommand::undo()
{
    if (_pOwner)
    {
        _pOwner->setPointValue(_stPoint, _nPreValue);
    }

    return;
}